package com.googlecode.garbagecan.jmsstudy.openjms;

import java.util.*;
import javax.jms.*;
import javax.naming.*;

public class TopicSubscribeSynchronous {

	public static void main(String[] args) {
		try {
			Hashtable properties = new Hashtable();
			properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.exolab.jms.jndi.InitialContextFactory");
			properties.put(Context.PROVIDER_URL, "rmi://localhost:1099/");
			Context context = new InitialContext(properties);
	
			TopicConnectionFactory factory = (TopicConnectionFactory) context.lookup("JmsTopicConnectionFactory");
			TopicConnection topicConnection = factory.createTopicConnection();
			topicConnection.start();
	
			TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
	
			Topic topic = (Topic) context.lookup("topic1");
			TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
			for (int i = 0; i < 10; i++) {
				TextMessage message = (TextMessage) topicSubscriber.receive();
				System.out.println("message[" + i + "]: " + message.getText());
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
